<?php
class item_changeloginname extends template_item
{
	var $default_name = "Change Your Login Name";
	var $default_description = "Change your your login name with this item";
	
	var $allow_multiple = 1;
	
	function public_use_form($inventory_ids = array())
	{
		$this->ipsclass->load_language('lang_market_items');
		
		$hidden_fields = $this->_get_hidden_fields($inventory_ids);
		
		return <<<EOF
<div class='formsubtitle'>{$this->ipsclass->lang['change_own_login']}</div>
<form action='{$this->base_url}' method='post'>
{$hidden_fields}
	<table class='ipbtable' cellspacing='0' cellpadding='0'>
		<tr>
			<td class='row1' align='left'>
				<b>{$this->ipsclass->lang['change_own_login_to']}</b>
			</td>
			<td class='row2' align='right'>
				<input type='input' name='member_name' value='{$this->ipsclass->member['name']}' size='30' class='forminput' />
			</td>
		</tr>
		<tr>
			<td class='darkrow1' align='center' colspan='2'>
				<input type='submit' name='item_used' value='{$this->ipsclass->lang['save_info']}' class='forminput' />
			</td>
		</tr>
	</table>
</form>
EOF;
	}
	
	function input_done()
	{
		return isset($this->ipsclass->input['member_name']) && isset($this->ipsclass->input['item_used']);
	}
	
	function public_use_item($inventory_ids = array()) 
	{
		$this->ipsclass->load_language('lang_market_items');
		
		$member_name = trim( $this->ipsclass->input['member_name'] );
		$banfilters   = array();
		
		if ( $this->ipsclass->vars['strip_space_chr'] )
		{
			// Use hexdec to convert between '0xAD' and chr
			$member_name = str_replace( chr(160), ' ', $member_name );
			$member_name = str_replace( chr(173), ' ', $member_name );
			$member_name = str_replace( chr(240), ' ', $member_name );
			
			$member_name = trim($member_name);
		}
		
		$unicode_name = preg_replace_callback('/&#([0-9]+);/si', create_function( '$matches', 'return chr($matches[1]);' ), $member_name);
		$unicode_name  = str_replace( "'" , '&#39;', $unicode_name );
		$unicode_name  = str_replace( "\\", '&#92;', $unicode_name );
		
		$this->ipsclass->DB->simple_construct( array( 'select' => '*', 'from' => 'banfilters' ) );
		$this->ipsclass->DB->simple_exec();
		
		while( $r = $this->ipsclass->DB->fetch_row() )
		{
			$banfilters[ $r['ban_type'] ][] = $r['ban_content'];
		}
		
		if ( ! $member_name OR strlen( $member_name ) < 3 AND strlen( $member_name ) > $this->ipsclass->vars['max_user_name_length'] )
		{
			$this->ipsclass->Error(array('MSG'=>'login_name_error_length'));
		}
		
		if ( preg_match( "#[\[\];,\|]#",  str_replace('&#39;', "'", $unicode_name ) ) )
		{
			$this->ipsclass->Error(array('MSG'=>'login_name_error_unicode'));
		}
		
		if ( is_array( $banfilters['name'] ) and count( $banfilters['name'] ) )
		{
			foreach ( $banfilters['name'] as $n )
			{
				if ( $n == "" )
				{
					continue;
				}
				
				if ( preg_match( "/".preg_quote($n, '/' )."/i", $member_name ) )
				{
					$this->ipsclass->Error(array('MSG'=>'login_name_found'));
					break;
				}
			}
		}
		
		$this->ipsclass->DB->build_query( array( 'select' => "members_display_name, id",
												 'from'   => 'members',
												 'where'  => "members_l_username='".strtolower($member_name)."' AND id != ".$this->ipsclass->member['id'],
												 'limit'  => array( 0,1 ) ) );
		$this->ipsclass->DB->exec_query();
		if ( $this->ipsclass->DB->get_num_rows() )
 		{
			$this->ipsclass->Error(array('MSG'=>'login_name_found'));
		}
		
		if ( $unicode_name != $member_name )
		{
			$this->ipsclass->DB->build_query( array( 'select' => "members_display_name, id, email",
													 'from'   => 'members',
													 'where'  => "members_l_username='".strtolower($unicode_name)."' AND id != ".$this->ipsclass->member['id'],
													 'limit'  => array( 0,1 ) ) );
			$this->ipsclass->DB->exec_query();
			
			if ( $this->ipsclass->DB->get_num_rows() )
			{
				$this->ipsclass->Error(array('MSG'=>'login_name_found'));
			}
		}
		
		$this->ipsclass->DB->do_update('members',array(
														'name' => $member_name,
														'members_l_username' => strtolower( $member_name )
				),"id=".$this->ipsclass->member['id']);
		
		if ( ! $this->ipsclass->vars['auth_allow_dnames'] )
		{
			$this->ipsclass->DB->do_update( 'members' , array( 'members_display_name'   => $member_name, 'members_l_display_name' => strtolower( $member_name ) ), "id=" .$this->ipsclass->member['id'] );
			
			$this->ipsclass->DB->do_update( 'contacts'      , array( 'contact_name'     => $member_name ), "contact_id="    .$this->ipsclass->member['id'] );
			$this->ipsclass->DB->do_update( 'forums'        , array( 'last_poster_name' => $member_name ), "last_poster_id=".$this->ipsclass->member['id'] );
			$this->ipsclass->DB->do_update( 'sessions'      , array( 'member_name'      => $member_name ), "member_id="     .$this->ipsclass->member['id'] );
			$this->ipsclass->DB->do_update( 'topics'        , array( 'starter_name'     => $member_name ), "starter_id="    .$this->ipsclass->member['id'] );
			$this->ipsclass->DB->do_update( 'topics'        , array( 'last_poster_name' => $member_name ), "last_poster_id=".$this->ipsclass->member['id'] );
			
			require_once( ROOT_PATH .'sources/action_admin/moderator.php' );
			$admod = new ad_moderator();
			$admod->ipsclass =& $this->ipsclass;
			
			$admod->rebuild_moderator_cache();
			
			require_once( ROOT_PATH.'sources/action_public/announcements.php' );
			$announcements = new announcements();
			$announcements->ipsclass =& $this->ipsclass;
			$announcements->announce_recache();
			
			$this->ipsclass->update_forum_cache();
		}
		
		if(USE_MODULES == 1)
		{
			require ROOT_PATH."modules/ipb_member_sync.php";
			$modules = new ipb_member_sync();
			$modules->register_class(&$this);
			$modules->on_name_change($this->ipsclass->member['id'],$member_name);
		}
		
		$this->ipsclass->lang['change_name_to_x'] = str_replace('<#NAME#>', $member_name, $this->ipsclass->lang['change_name_to_x']);
		
		$this->delete_items($inventory_ids);
		
		$inv_id = reset($inventory_ids);
		$this->_write_log($inv_id, "Changed login name from {$this->ipsclass->member['name']} to {$member_name}");
		
		$this->ipsclass->market_class->redirect('change_name_to_x');
	}
}
?>